'use strict';

/* Controllers */
app.controller('personalCtrl', function($scope, ngDialog, uiLoad, rest, user, dialog, $state, uploadConf, tipService) {

/* 业主业主业主业主业主业主业主业主业主房东
对应接口编号： 4005增； 4023改； 4024查；

房东姓名
    data.landlordName
房东身份证号
    data.idCardNo
房东收款银行
    data.bankName
房东收款银行卡号
    data.bankAccount
业主（房东）电话号码                业主（房东）电话号码（线上填写）
    data.phone
业主（房东）身份证正反面            业主（房东）身份证正反面（线上影像件）
    data.idCard
    data.idCardBack
业主（房东）委托人电话号码           业主（房东）委托人电话号码（线上填写）
    data.clientPhone
业主（房东）委托人身份证正反面       业主（房东）委托人身份证正反面（线上影像件）
    data.clientCard
    data.clientCardBack
委托代理人授权书                    委托代理人授权书（线上影像件）
    data.entrust
房产证复印件或者有效期15天内的产调    房产证复印件或者有效期15天内的产调（线上影像件）
    data.house
《个人信用报告查询授权书》           业主征信获取授权书（电签）
    agreement

相关物业办理的宽带申请资料？
*/



  $scope.isShow = false;

  $scope.verifyFailMessage = "";
  $scope.verifying = false;
  $scope.verifyFail = false;
  $scope.verifyFail1 = false;
  $scope.agreement = true;

  $scope.requestMethodId = "4005";
  $scope.checkId = undefined;

  $scope.data = {
    landlordId: user.get('landlordId')
  };

  $scope.column2 = 0;      //分栏
  $scope.maxImgNum = 100;    //最大图片数量
  $scope.imgNew= [
      {
          text:'业主（房东）身份证正反面',
          name: 'landlordPicChange',
          value: [],
          danger: 1
      },
      {
          text:'业主（房东）委托人身份证正反面',
          name: 'landlordClientPicChange',
          value: [],
      },
      {
          text:'委托代理人授权书',
          name: 'clientPicChange',
          value: [],
      },
      {
          text:'房产证复印件或者有效期15天内的产调',
          name: 'housePropertyCertificateChange',
          value: [],
      }
    ];   
       
  uploadConf.column2 = $scope.column2;
  uploadConf.maxImgNum =  $scope.maxImgNum;
  uploadConf.imgNew = $scope.imgNew;

  rest.post({
    methodId: '4012',
    data: {
      landlordId: user.get('landlordId')
    },
    success: function(data) {
      if (data.code == "0") {
        var data = data.res.data;
        $scope.checkId = data.checkId;
        // status  -1=未申请 0=申请中 1=审核通过 2=审核不通过

        // identityType [0=未申请 1=业主 2=二房东（个人） 3=二房东（公司）]
        // 17.3.4 高峰：房东身份变更先不做。彭志军：只管0表示未申请，1 2 3 不要管

        if(data.identityType == 0 || data.identityType == 1 ){

          if (data.status == 0) {
            $scope.steps.step2 = true;
            $scope.verifying = true;
            // $scope.isDisabled1 = true;
          } else if (data.status == 1) {
            $scope.steps.step3 = true;
            // $scope.isDisabled1 = true;
          } else if(data.status == -1){
            init();
            $scope.steps.step1 = true;
          } else if(data.status == 2){
            $scope.verifyFail1 = true;
            $scope.steps.step2 = true;
            $scope.requestMethodId = "4023";
            $scope.verifyFailMessage = data.remark;
            $scope.checkId = data.checkId;
            getPersonalInfo();
          }else if(data.status == 3){
            $scope.verifyFail = true;
            $scope.steps.step2 = true;
            $scope.requestMethodId = "4023";
            $scope.verifyFailMessage = data.remark;
            $scope.checkId = data.checkId;
            getPersonalInfo();
          }
        } else {
          init();
          $scope.steps.step1 = true;
        }
        $scope.isShow = true;
        $scope.$digest();
      } else {
        dialog.tips(data.res.msg);
      }
    },
    error: function() {}
  });

  $scope.update = function(){
    $scope.steps.step1 = true;
  }

  /**
    上传业主房东身份证照片
  */
  $scope.$on('landlordPicChange', function(d, data) {
    $scope.data.idCard = data.list[0];
    $scope.data.idCardBack = data.list[1];
  });
  /**
    上传业主（房东）委托人身分证照片
  */
  $scope.$on('landlordClientPicChange', function(d, data) {
    $scope.data.clientCard = data.list[0];
    $scope.data.clientCardBack = data.list[1];
  });

  /**
   *  上传委托人代理人授权书
   */
  $scope.$on('clientPicChange', function(d, data) {
    $scope.data.entrust = data.list;
  });

  // 上传房屋产权证书照片
  $scope.$on('housePropertyCertificateChange', function(d, data) {
    $scope.data.house = data.list;
  });

  // 提交个人业主数据
  $scope.personalInfo = function() {
    $scope.verifying = false;
    $scope.verifyFail = false;
    $scope.verifyFail1 = false;

    console.log($scope.data);
    console.log(uploadConf.imgNew);
    $scope.data.idCard = {};
    $scope.data.idCardBack = {};
    $scope.data.clientCard = {};
    $scope.data.clientCardBack = {};
    $scope.data.entrust = [];
    $scope.data.house = [];

    $scope.data.idCard = uploadConf.imgNew[0].value[0];
    $scope.data.idCardBack = uploadConf.imgNew[0].value[1];
    $scope.data.clientCard = uploadConf.imgNew[1].value[0];
    $scope.data.clientCardBack = uploadConf.imgNew[1].value[1];
    for (var i in uploadConf.imgNew[2].value) {
        $scope.data.entrust.push(uploadConf.imgNew[2].value[i]);
    }
    for (var i in uploadConf.imgNew[3].value) {
        $scope.data.house.push(uploadConf.imgNew[3].value[i]);
    }
    for (var i in $scope.imgNew) {
       if ($scope.imgNew[0].value.length < 2) {
        tipService.show("body", {
            message: "业主（房东）身份证正反面照片必传",
            notice: "danger"
        }, 3);
        return;
      }
    }
    if($scope.data.phone.length != 11 || $scope.data.phone.charAt(0) != 1){
        tipService.show("body", {
            message: "业主（房东）电话号码格式有误",
            notice:"danger"
        }, 3);
        return;
    }
    if ($scope.data.clientPhone){
        if($scope.data.clientPhone.length != 11 || $scope.data.clientPhone.charAt(0) != 1){
            tipService.show("body", {
                message: "业主（房东）委托人电话号码格式有误",
                notice:"danger"
            }, 3);
            return;
        }
    }
    $scope.data.landlordName = $scope.data.landlordName.replace(/[ 　\[\]\\\/\"\'\-!@#$%^&*(){}<>,.?:;~`_+=]/g, "");
    
    rest.post({
      methodId: $scope.requestMethodId,
      log: true,
      data: $scope.data,
      success: function(data) {
        if (data.code == "0") {
          $scope.steps.step2 = true;
          $scope.verifying = true;
          $scope.$digest();
        } else {
          dialog.tips(data.res.msg)
        }
      },
      error: function() {}
    });
  }

  $scope.goHouseEdit = function() {
    // http://120.26.246.115:8020/index.html?landlordId=10039
    if (user.get('landlordId')) {
      $state.go('app.houseOwner.list');
      // location.href = "http://120.26.246.115:8020/index.html?landlordId=" + user.get('landlordId');
    }
    // console.log('goHouseEdit: %s', user.get('landlordId'));
  }

  $scope.goBack = function(){
    $state.go('nav.widget')
  }

  $scope.showlandlordPicTips = function(){
    dialog.tips("身份证拍照必须正反面清晰，无光线遮挡，无缺角与遮挡。（纸质与影像件）")
  }
  $scope.showlandlordClientPicTips = function(){
    dialog.tips("业主（房东）委托人身份证正反面（线上影像件）")
  }
  $scope.showclientPicTips = function(){
    dialog.tips("")
  }
  $scope.showhousePropertyCertificateTips = function(){
    dialog.tips("1.产权证必须清晰可辨，信息完整无缺页，附件有文字信息的也同时提供。<br/> 2.拆迁补偿类暂未办理产证房源，必须提供拆迁补偿协议，协议中明确体现补偿房屋坐落位置，面积，被补偿人，拆迁单位，房源开发商等信息，并且合同中所涉及企业与个人都有签字或盖章。<br/> 3.国有单位或军队公务员置换安置协议，必须提供提供置换安置协议，协议中明确体现补偿房屋坐落位置，面积，被置换安置人，置换安置单位，房产提供单位等信息，并且合同中所涉及单位与个人都有签字或盖章。")
  }

  function init(data){
    /**
     * 个人业主
     * 4005
     */
    
    $scope.data = {
      landlordId: user.get('landlordId'),
      landlordName: !!data ? data.landlordName : "",
      idCardNo: !!data ? data.idCardNo : "",
      bankName: !!data ? data.bankName : "",
      bankAccount: !!data ? data.bankAccount : "",
      phone: !!data ? data.phone : "",
      clientPhone: !!data ? data.clientPhone : "",
      idCard: !!data ? data.idCard : "",
      idCardBack: !!data ? data.idCardBack : "",
      entrust: !!data ? data.entrust : "",
      house: !!data ? data.house : "",
      checkId: $scope.checkId
    };
    // if (data.clientCard) {
    //   $scope.data.clientCard = data.clientCard;
    // }
    // if (data.clientCardBack) {
    //   $scope.data.clientCardBack = data.clientCardBack;
    // }
    /**
      上传业主房东身份证照片
    */
    // $scope.landlordPic = !!data ? [data.idCard,data.idCardBack] : [];
    /**
      上传业主（房东）委托人身分证照片
    */
    // $scope.landlordClientPic = !!data ? [data.clientCard,data.clientCardBack] : [];
    /**
     *  上传委托人代理人授权书
     */
    // $scope.clientPic = !!data && data.entrust ? data.entrust : [];
    /**
    *  上传房屋产权证书照片
    */
    // $scope.housePropertyCertificate = !!data && data.house ? data.house : [];
    if (data) {
      // 上传二房东（法人）身份证照片
      $scope.imgNew[0].value = [];
      if (data.idCard)
          $scope.imgNew[0].value.push(data.idCard);
      if (data.idCardBack)
          $scope.imgNew[0].value.push(data.idCardBack);
      // // 上传二房东业务负责人身份证
      $scope.imgNew[1].value = [];
      if (data.clientCard)
          $scope.imgNew[1].value.push(data.clientCard);
      if (data.clientCardBack)
          $scope.imgNew[1].value.push(data.clientCardBack);
      // 上传营业执照
      $scope.imgNew[2].value = data.entrust;
      // 上传税务登记证与组织机构代码证 CodeCertificate
      $scope.imgNew[3].value = data.house;
    }
  }
  function getPersonalInfo(){
    rest.post({
      methodId: '4024',
      data: {
        id: $scope.checkId
      },
      success: function(data) {
        if (data.code == "0") {
          init(data.res.data);
        } else {
          dialog.tips(data.res.msg);
        }
      },
      error: function() {}
    });
  }

  $scope.showTxt = function(contractId){
        ngDialog.openConfirm({
          template: 'js/module/fund/creditReport.html',
          resolve: {},
          data:{},
          scope: $scope,
          width: 800,
          height: 700
        }).then(function(data) {

        }, function(reason) {

        });
    }
});
